{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np # 导入Numpy模块并取别名为np\n",
    "import random # 导入随机数模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "5\n",
      "3\n",
      "一维向量: [1]\n",
      "二维向量: [1, 2]\n",
      "四维向量: [2, 7, 7]\n",
      "N等于: 10\n",
      "随机生成的N维向量: [0, 8, 6, 4, 1, 0, 8, 5, 1, 7]\n"
     ]
    }
   ],
   "source": [
    "# 定义一个python中的list用于表示向量\n",
    "# list的长度表示向量的维度\n",
    "# 例如：只有1个元素的list 则相当于 一维向量\n",
    "list1 = [1]\n",
    "\n",
    "# 二维向量：包含两个元素的list\n",
    "list2 = [1,2]\n",
    "\n",
    "# 四维向量：包含四个元素的list\n",
    "list4 = [1,2,3,4]\n",
    "# ......\n",
    "# N维向量：包含N个元素的list\n",
    "# 使用随机数模块及range函数随机结合列表推导式生成N维向量\n",
    "\n",
    "# 生成0-9的随机整数\n",
    "print(random.randint(0,9))\n",
    "print(random.randint(0,9))\n",
    "print(random.randint(0,9))\n",
    "\n",
    "# 列表推导式：通过一个表达式来生成list\n",
    "\n",
    "n = 10 # n值可以任意修改，这里设置为10 表示即将生成一个维度为10的向量\n",
    "listN = [random.randint(0,9) for i in range(n)]\n",
    "\n",
    "\n",
    "# 打印\n",
    "print('一维向量:',list1)\n",
    "print('二维向量:',list2)\n",
    "print('四维向量:',list3)\n",
    "\n",
    "print('N等于:',n)\n",
    "print('随机生成的N维向量:',listN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机生成的三维向量为： [2, 7, 7]\n"
     ]
    }
   ],
   "source": [
    "# 随机生成一个三维向量\n",
    "list3 = [random.randint(0,9) for i in range(3)]\n",
    "print('随机生成的三维向量为：',list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 7 7] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "# 向量的范数\n",
    "\n",
    "# 先将随机生成的三维向量转换为numpy包中的ndarray类型（数据结构）\n",
    "# 转换为ndarray类型后 可以直接使用numpy包提供的方法计算向量的范数\n",
    "list3_nd = np.array(list3)\n",
    "# 打印向量及类型\n",
    "print(list3_nd,type(list3_nd))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16.0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# linalg=linear（线性）+algebra（代数）\n",
    "# norm则表示范数\n",
    "\n",
    "# np.linalg.norm(x,ord) numpy中求范数的方法\n",
    "# 支持两个参数：\n",
    "# x：可以传入一个ndarray类型的变量\n",
    "# ord：表示计算的范式类型，可以选择以下几个值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0-范数 向量非零元素的个数\n",
    "np.linalg.norm(list3_nd,ord=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16.0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1-范数 即向量元素绝对值之和，x 到零点的曼哈顿距离\n",
    "np.linalg.norm(list3_nd,ord=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.099504938362077"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2-范数 也称为Euclid范数（欧几里得范数，常用计算向量长度）\n",
    "# 即向量元素绝对值的平方和再开方，表示x到零点的欧式距离\n",
    "np.linalg.norm(list3_nd,ord=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.502415597601675"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# p-范数 即向量元素绝对值的p次方和的1/p次幂，表示x到零点的p阶闵氏距离\n",
    "p = 10 # 设p等于10\n",
    "np.linalg.norm(list3_nd,ord=10) # 10范数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.0"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ∞-范数 当p趋向于正无穷时，即所有向量元素绝对值中的最大值\n",
    "# np.inf : 正无穷 ∞\n",
    "np.linalg.norm(list3_nd,ord=np.inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# -∞-范数 当p趋向于负无穷时，即所有向量元素绝对值中的最小值\n",
    "np.linalg.norm(list3_nd,ord=-np.inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
